package com.talkweb.securitycamera.utils;

import ohos.agp.components.AttrHelper;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

public final class DensityUtils {

    /**
     * 屏幕宽度(px)
     */
    public static int SCREEN_WIDTH_PIXELS;

    /**
     * 屏幕高度(px)
     */
    public static int SCREEN_HEIGHT_PIXELS;

    /**
     * 屏幕实际宽度(px)
     */
    public static int SCREEN_REAL_WIDTH_PIXELS;

    /**
     * 屏幕实际高度(px),除去标题、状态栏
     */
    public static int SCREEN_REAL_HEIGHT_PIXELS;

    public static int SCREEN_POINT_WIDTH_PIXELS;

    public static int SCREEN_POINT_HEIGHT_PIXELS;

    /**
     * 屏幕密度(px) 1080 * 2340 = 3.0
     */
    public static float SCREEN_DENSITY = 0f;

    /**
     * text屏幕密度(px) 1080 * 2340 = 3.0
     */
    public static float TEXT_SCALED_DENSITY = 0f;

    private static boolean mInitialed = false;



    private DensityUtils() {
    }

    public static void init(Context context) {
        if (mInitialed || context == null) {
            return;
        }
        mInitialed = true;
        Display display = getDisplay(context);
        DisplayAttributes dm = display.getRealAttributes();
        SCREEN_WIDTH_PIXELS = dm.width;
        SCREEN_HEIGHT_PIXELS = dm.height;
//        与densityPixels值一致
//        SCREEN_DENSITY = AttrHelper.getDensity(context);
        SCREEN_DENSITY = dm.densityPixels;
        TEXT_SCALED_DENSITY = dm.scalDensity;
        DisplayAttributes dmr = display.getAttributes();
        SCREEN_REAL_WIDTH_PIXELS = dmr.width;
        SCREEN_REAL_HEIGHT_PIXELS = dmr.height;

        Point point = new Point();
        display.getSize(point);

        SCREEN_POINT_WIDTH_PIXELS = point.getPointXToInt();
        SCREEN_POINT_HEIGHT_PIXELS = point.getPointYToInt();
        print(dm);
    }

    private static void print(DisplayAttributes attr) {
        StringBuilder sb = new StringBuilder();
        sb.append("屏幕参数：\n");
        sb.append("sw=");
        sb.append(attr.width);
        sb.append(", sh=");
        sb.append(attr.height);
        sb.append(", density(px,dpi)=(");
        sb.append(attr.densityPixels);
        sb.append(",");
        sb.append(attr.densityDpi);
        sb.append("), scaleDensity=");
        sb.append(attr.scalDensity);
        sb.append(",xDpi=");
        sb.append(attr.xDpi);
        sb.append(",yDpi=");
        sb.append(attr.yDpi);
        sb.append(", sd=");
        sb.append(SCREEN_DENSITY);
        sb.append(",vp2px=");
        sb.append(vp2px(10));
        sb.append(",px2vp=");
        sb.append(px2vp(10));
        System.out.println(sb.toString());


    }

    /**
     * 根据手机的分辨率从 vp 的单位 转成为 px(像素)
     *
     * @param vpValue
     * @return
     */
    public static int vp2px(float vpValue) {
        return vp2px(null, vpValue);
    }

    /**
     * 根据手机的分辨率从 vp 的单位 转成为 px(像素)
     *
     * @param context 上下文
     * @param vpValue vp的值
     * @return 转换成px的值
     */
    public static int vp2px(Context context, float vpValue) {
        if (SCREEN_DENSITY == 0) {
            init(context);
        }

        final float scale = SCREEN_DENSITY;
        return AttrHelper.vp2px(vpValue, scale);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 vp
     *
     * @param pxValue
     * @return
     */
    public static int px2vp(float pxValue) {
        return px2vp(null, pxValue);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 vp
     *
     * @param context 上下文
     * @param pxValue 待转换的px的值
     * @return 转换成vp的值
     */
    public static int px2vp(Context context, float pxValue) {
        if (SCREEN_DENSITY == 0) {
            init(context);
        }
        final float scale = SCREEN_DENSITY;
        return Float.valueOf(pxValue / scale).intValue() ;
    }

    /**
     * 将fp值转换为px值，保证文字大小不变
     *
     * @param fpValue
     * @return
     */
    public static int fp2px(float fpValue) {
        return fp2px(null, fpValue);
    }

    /**
     * 将fp值转换为px值，保证文字大小不变
     *
     * @param context 上下文
     * @param fpValue
     * @return
     */
    public static int fp2px(Context context, float fpValue) {
        if (TEXT_SCALED_DENSITY == 0) {
            init(context);
        }
        final float fontScale = TEXT_SCALED_DENSITY;
        return AttrHelper.fp2px(fpValue, fontScale);
    }


    private static Display getDisplay(Context ct) {
        if (ct == null) {
            throw new NullPointerException("init display object need context not be null.");
        }
        return DisplayManager.getInstance().getDefaultDisplay(ct).get();
    }

    public static int[] getScreenSize(Context ct) {
        if (!mInitialed) {
            init(ct);
        }
        return new int[]{SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS};
    }

    public static int[] getScreenRealSize(Context ct) {
        if (!mInitialed) {
            init(ct);
        }
        return new int[]{SCREEN_REAL_WIDTH_PIXELS, SCREEN_REAL_HEIGHT_PIXELS};
    }

}
